//
//  XJCommentFooterView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/20.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJCommentFooterView.h"
#import "XJAfterSalePhotItem.h"

@interface XJCommentFooterView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *clv;
@property (nonatomic, strong) UIButton *commentButton;
@end

@implementation XJCommentFooterView

#pragma mark - 一 lazy
- (UICollectionView *)clv {
    if (!_clv) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        _clv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 100) collectionViewLayout:layout];
        _clv.backgroundColor = [UIColor whiteColor];
        
        _clv.delegate = self;
        _clv.dataSource = self;
        
        UINib * nibCell = [UINib nibWithNibName:@"XJAfterSalePhotItem" bundle:nil];
        [_clv registerNib:nibCell forCellWithReuseIdentifier:@"XJAfterSalePhotItem"];
    }
    return _clv;
}

- (UIButton *)commentButton {
    if (!_commentButton) {
        _commentButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_commentButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_commentButton setTitle:@"提交评论" forState:(UIControlStateNormal)];
        _commentButton.backgroundColor = XJCOLOR_RGB(255, 218, 67);
        [_commentButton addTarget:self action:@selector(commentButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        _commentButton.layer.cornerRadius = 10;
        _commentButton.layer.masksToBounds = YES;
    }
    return _commentButton;
}


- (NSMutableArray *)imageArray {
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

#pragma mark - 二 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView {
    [self addSubview:self.clv];
    
    [self addSubview:self.commentButton];
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat k15 = 15.0;
    [self.clv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(k15);
        make.right.equalTo(self.mas_right).offset(-k15);
        make.top.equalTo(self);
        make.height.mas_equalTo(100);
    }];
    
    [self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.clv.mas_bottom).offset(100);
        make.left.equalTo(self.mas_left).offset(k15);
        make.right.equalTo(self.mas_right).offset(-k15);
        make.height.mas_equalTo(50);
    }];
}

#pragma mark - 四 UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.imageArray.count + 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XJAfterSalePhotItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"XJAfterSalePhotItem" forIndexPath:indexPath];
    
    if (indexPath.row == self.imageArray.count) {
        item.profileIv.image = [UIImage imageNamed:@"plus.png"];
        item.cancelButton.hidden = YES;
    }else {
        item.profileIv.image = self.imageArray[indexPath.row];
        item.cancelButton.hidden = NO;
    }
    
    return item;
}

#pragma mark - 四 UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.imageArray.count) {
        
    // 调用系统相册、拍照功能。
    }
}

#pragma mark - 四 UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat wh = (KWidth - 64) / 4;
    
    return CGSizeMake(wh, wh);
}


#pragma mark - 五 点击事件
- (void)commentButtonAction {
    XJLog(@"提交");
    
    if (self.commentButtonBlock) {
        self.commentButtonBlock();
    }
}

@end
